package sanphone.com.runningschool.Saying;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import sanphone.com.runningschool.BaseActivity;
import sanphone.com.runningschool.ForceOffline;
import sanphone.com.runningschool.InterfaceLocation;
import sanphone.com.runningschool.LogIn.LogInActivity;
import sanphone.com.runningschool.MainActivity;
import sanphone.com.runningschool.R;
import sanphone.com.runningschool.RequestHttp.RequestHttp;

public class SayingActivity extends BaseActivity {
    private Button submitButton;
    private EditText contentText,titleText;
    private String title,content,idStr,sendString,sendTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saying);
        SharedPreferences sprf = getSharedPreferences("data", MODE_PRIVATE);
        ForceOffline offline=new ForceOffline(sprf.getString("schoolId",null),this);
        offline.check();
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        initView();
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                title=titleText.getText().toString();
                content=contentText.getText().toString();
                if(title.equals("")||content.equals("")){
                    Toast.makeText(v.getContext(),"内容或者标题不能为空!",Toast.LENGTH_SHORT).show();
                }
                else{
                    try {
                         sendString=  URLEncoder.encode(content,"UTF-8");
                         sendTitle= URLEncoder.encode(title,"UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    //开启线程池
                    ExecutorService executorService= Executors.newSingleThreadExecutor();
                    executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                            JSONObject json=new JSONObject();
                            try {
                                json.put("questionTitle",sendTitle);
                                json.put("questionText",sendString);
                                json.put("userId",idStr);
                              RequestHttp requestHttp=new RequestHttp(InterfaceLocation.getLocation()+"sendHelp",SayingActivity.this);
                              String request=requestHttp.post(json);
                              JSONObject requestJson=new JSONObject(request);

                              if(requestJson.getString("data").equals("问题提问成功")){
                                  runOnUiThread(new Runnable() {
                                      @Override
                                      public void run() {
                                          Toast.makeText(SayingActivity.this,"问题提问成功",Toast.LENGTH_SHORT).show();

                                      }
                                  });
                                  startActivity(new Intent(SayingActivity.this, MainActivity.class));
                              }else if(requestJson.getString("data").equals("要有礼貌哦")){
                                  runOnUiThread(new Runnable() {
                                      @Override
                                      public void run() {
                                          Toast.makeText(SayingActivity.this,"说话要有礼貌噢！",Toast.LENGTH_SHORT).show();

                                      }
                                  });
                              }
                              else if(requestJson.getString("data").equals("没有登录或登录已过期")){
                                  runOnUiThread(new Runnable() {
                                      @Override
                                      public void run() {
                                          Toast.makeText(SayingActivity.this,"没有登录或登录已过期",Toast.LENGTH_SHORT).show();

                                      }
                                  });
                                  //startActivity(new Intent(SayingActivity.this,LogInActivity.class));
                              }
                              else {
                                  runOnUiThread(new Runnable() {
                                      @Override
                                      public void run() {
                                          Toast.makeText(SayingActivity.this,"发生未知错误",Toast.LENGTH_SHORT).show();

                                      }
                                  });

                              }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }
                    });
                    executorService.shutdown();
                }
            }
        });
    }

    private void initView() {
        submitButton=findViewById(R.id.question_submit);
        titleText=findViewById(R.id.p_title);
        contentText=findViewById(R.id.p_content);
        SharedPreferences pref=getSharedPreferences("data",MODE_PRIVATE);
        idStr = pref.getString("schoolId", null);
        Log.e("sayingActivity", idStr );


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                SayingActivity.this.finish();
                break;
            default:
                break;
        }
        return true;
    }
}
